package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 3D柱状图
 *
 * @author chart
 */
@RestController
@RequestMapping("/bar3D/chart")
public class Bar3DChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;



    //多排3D柱状图
    @PostMapping("/multi3DBar/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取excel读取到的数据
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            //处理数据
            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> ss = getdata.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            //防止出现为空值或为空字符的数值
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdata.get(i).remove(0);
                        dataList.add(getdata.get(i));
                    }
                }
            }


//            yList.remove(0);

            List<ChartXDataVo> xData = new ArrayList<>();

            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String x = xList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }


            //获取到对应图表的配置，将它反序列化
            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);

            //获取图例配置
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(0);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("datasetIndex", i);
                o1.put("name", yList.get(i));
                seriesList.add(o1);

                ChartLegendData data = new ChartLegendData();
                data.setName(yList.get(i));
                chartLegendData.add(data);
            }
            jsonObject.put("series", seriesList);

            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
//            ChartXDataVo chartXDataVo = xData.get(0);
//            int size = chartXDataVo.getSource().size();
//            int i = size * 8;

            //根据数据多少将x,y轴进行按比例放大
            if (xList.size()>10 || yList.size()>10) {
                grid3D.put("boxWidth", xList.size()*8);
                grid3D.put("boxDepth", yList.size()*8);

            }else if (xList.size()<=10 && yList.size()<=10){
                grid3D.put("boxWidth", xList.size()*20);
                grid3D.put("boxDepth", yList.size()*20);
            }

            jsonObject.put("grid3D", grid3D);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //多排切面3D柱状图
    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();

            List<Object> pointCuts = new ArrayList<>();
            List<String> sections = getdata.get(1);
            getdata.remove(1);
            for (int i = 1; i < sections.size(); i++) {
                String s = sections.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pointCuts.add(Double.parseDouble(s));
                }
            }



            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> ss = getdata.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdata.get(i).remove(0);
                        dataList.add(getdata.get(i));
                    }
                }
            }


//            yList.remove(0);

            List<ChartXDataVo> xData = new ArrayList<>();

            List<List<Object>> newDataLists = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String x = xList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));

                        List<Object> cellList1 = new ArrayList<>();
                        cellList1.add(x);
                        cellList1.add(y);
                        cellList1.add(Double.parseDouble(s));
                        newDataLists.add(cellList1);
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);


            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(0);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("datasetIndex", i);
                o1.put("name", yList.get(i));
                seriesList.add(o1);

                ChartLegendData data = new ChartLegendData();
                data.setName(yList.get(i));
                chartLegendData.add(data);
            }


            JSONObject o1 = series.get(3);
            for (int i = 0; i < pointCuts.size(); i++) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(o1);
                List<List<Object>> list = new ArrayList<>();
                for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                    List<Object> objects = newDataLists.get(i1);
                    List<Object> objects1 = new ArrayList<>();
                    objects1.addAll(objects);
                    objects1.set(2,pointCuts.get(i));
                    list.add(objects1);
                }
                jsonObject1.put("data",list);
                seriesList.add(jsonObject1);
            }


            jsonObject.put("series", seriesList);


            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");


            if (xList.size()>10 || yList.size()>10) {
                grid3D.put("boxWidth", xList.size()*8);
                grid3D.put("boxDepth", yList.size()*8);

            }else if (xList.size()<=10 && yList.size()<=10){
                grid3D.put("boxWidth", xList.size()*20);
                grid3D.put("boxDepth", yList.size()*20);
            }


            jsonObject.put("grid3D", grid3D);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


}
